﻿namespace UFIDA.U9.Cust.SH.SHFastShipBP
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFIDA.U9.SM.SO;
    using UFIDA.U9.Cust.SH.SHFastShipBP.SHFastShipBP;
    using UFIDA.U9.CBO.SCM.Item;
    using UFIDA.U9.CBO.SCM.Warehouse;
    using UFIDA.U9.Base;
    using UFIDA.U9.CBO.SCM.Customer;

	/// <summary>
	/// GetSOLineInfo partial 
	/// </summary>	
	public partial class GetSOLineInfo 
	{	
		internal BaseStrategy Select()
		{
			return new GetSOLineInfoImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class GetSOLineInfoImpementStrategy : BaseStrategy
	{
		public GetSOLineInfoImpementStrategy() { }

		public override object Do(object obj)
		{						
			GetSOLineInfo bpObj = (GetSOLineInfo)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	
			
			//auto generating code end,underside is user custom code
			//and if you Implement replace this Exception Code...
			//throw new NotImplementedException();

            if (bpObj.SOLineIDs.Count <= 0) return null;
            List<ItemInfoDTO> retlist = new List<ItemInfoDTO>();

            foreach (long id in bpObj.SOLineIDs)
            {
                SOLine line = SOLine.Finder.FindByID(id);
                if (line == null) throw new Exception("查找订单出错!");

                //销售订单行存储的料品是原组织的料品ID，需要进行转换
                string opath = "1=1 ";
                opath += " and Code='" + line.ItemInfo.ItemCode + "'";
                opath += " and Org=" + Context.LoginOrg.ID;
                ItemMaster item = ItemMaster.Finder.Find(opath);
                if (item == null) throw new Exception("查找料品"+line.ItemInfo.ItemCode+"出错！");

                ItemInfoDTO retdto = new ItemInfoDTO();
                retdto.SOLineID = id;
                retdto.ItemID = item.ID;
                retdto.ItemCode = item.Code;
                retdto.ItemName = item.Name;
                retdto.ItemSpec = item.SPECS;
                retdto.SODocNo = line.SO.DocNo;
                
                //单头备注
                //if(line.SO.SOMemoes.Count>0)
                //{
                //    retdto.Memo = line.SO.SOMemoes[0].Description;
                //}

                retdto.Memo = line.SO.DescFlexField.PubDescSeg41;

                //获取仓库逻辑
                //优先销售订单行仓库;再取料品：销售页签：出货地点
                Warehouse wh;
                if (line.SOShiplines.Count > 0 && line.SOShiplines[0].WHKey != null)
                {
                    wh = line.SOShiplines[0].WH;
                }
                else
                {
                    wh = PubTool.GetDefaultWH(bpObj.WH, item.ID, bpObj.CustID);
                }
                retdto.WH = wh.ID;
                retdto.WHCode = wh.Code;
                retdto.WHName = wh.Name;

                //获取计量单位
                if (item.InventoryUOM != null)
                {
                    retdto.UOM = item.InventoryUOM.ID;
                    retdto.UOMCode = item.InventoryUOM.Code;
                    retdto.UOMName = item.InventoryUOM.Name;
                }

                
                //设置数量单价
                retdto.Qty = line.OrderByQtyTU-line.SOLineSumInfo.SumShipQtyTU;
                if (retdto.Qty == 0) throw new Exception("销售订单："+line.SO.DocNo+"料号："+line.ItemInfo.ItemCode+"数量为0！");
                retdto.ProductPrice = PubTool.ToDecimal(line.DescFlexField.PubDescSeg39) / line.OrderByQtyTU;
                if (retdto.ProductPrice == 0)
                    retdto.ProductPrice = line.FinallyPriceTC ;
                retdto.MarkPrice = PubTool.ToDecimal(line.DescFlexField.PubDescSeg38) / line.OrderByQtyTU;
                retdto.TransportPrice = PubTool.ToDecimal(line.DescFlexField.PubDescSeg37) / line.OrderByQtyTU;
                retdto.StorePrice = PubTool.ToDecimal(line.DescFlexField.PubDescSeg36) / line.OrderByQtyTU;
                //if (Context.LoginOrg.Code == "1000")
                //{
                //    //双虎组织
                //    retdto.Price = retdto.ProductPrice + retdto.MarkPrice + retdto.TransportPrice;
                //}
                //else if (Context.LoginOrg.Code.StartsWith("2"))
                //{
                //    //总经销
                //    retdto.Price = retdto.ProductPrice + retdto.MarkPrice + retdto.TransportPrice + retdto.StorePrice;
                //}
                //else
                //{
                //    retdto.Price = 0;
                //}
                //直接取订单价格
                retdto.Price = line.FinallyPriceTC;
                retdto.Weight = item.Weight*retdto.Qty;

                //正特价
                //if (line.DescFlexField.PubDescSeg50.Trim() == "0")
                //    retdto.PriceType = "正价";
                //else if (line.DescFlexField.PubDescSeg50.Trim() == "1")
                //    retdto.PriceType = "特价";
                //else
                //    retdto.PriceType = line.DescFlexField.PubDescSeg50;
                retdto.PriceType = line.DescFlexField.PubDescSeg50.Trim();

                //门店新增字段
                retdto.Deposit = PubTool.ToDecimal(line.DescFlexField.PubDescSeg30);
                retdto.MDDocNo = line.SO.DescFlexField.PubDescSeg26;
                //retdto.MDMemo = line.SO.DescFlexField.PubDescSeg28;
                retdto.MDItemCode = line.DescFlexField.PrivateDescSeg8;
                retdto.MDLineID = line.DescFlexField.PrivateDescSeg10;
                retdto.SalePrice = PubTool.ToDecimal(line.DescFlexField.PrivateDescSeg11);
                retdto.OrderQty = PubTool.ToDecimal(line.DescFlexField.PrivateDescSeg12);

                //沙发相关属性
                if (item.InventoryInfo.LotParamKey != null)
                {
                    if (line.SOShiplines[0].Lot == null || string.IsNullOrEmpty(line.SOShiplines[0].Lot.LotCode))
                        throw new Exception("销售订单："+line.SO.DocNo+"料号："+item.Code+" 不存在批号！"); 
                    retdto.Lot = line.SOShiplines[0].Lot.LotCode;
                    retdto.LotID = line.SOShiplines[0].Lot.LotMasterKey.ID;
                    retdto.SFSpec = line.DescFlexField.PubDescSeg45;
                    retdto.SFMaterial = line.DescFlexField.PubDescSeg46;
                    retdto.Direction = line.DescFlexField.PubDescSeg47;
                    retdto.SFCount = line.DescFlexField.PubDescSeg44;
                    //下线客户
                    if (!string.IsNullOrEmpty(line.DescFlexField.PubDescSeg40))
                    {
                        //retdto.LastCustomer = line.DescFlexField.PubDescSeg40;
                        Customer cu = Customer.Finder.Find("Org="+Context.LoginOrg.ID+" and Code='" + line.DescFlexField.PubDescSeg40.Trim()+"'");
                        if (cu != null)
                            retdto.LastCustomer = cu.Name;
                    }

                    
                }

                //设置库存数量、可用量
                PubTool.SetKitQty1(retdto);

                retlist.Add(retdto);
            }
            return retlist;
		}

	
	}

	#endregion
	
	
}